package com.tianbao54.thrift.client;

import com.tianbao54.thrift.generated.User;
import com.tianbao54.thrift.generated.UserService;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;

public class ThriftClient {

    public static void main(String[] args) {

        TTransport transport = new TFramedTransport(new TSocket("127.0.0.1",8888));
        TProtocol protocol = new TCompactProtocol(transport);
        UserService.Client client = new UserService.Client(protocol);

        try {

            transport.open();
            User user = client.receiveMessage();
            System.out.println("client = " + user);
            System.out.println("-----------------");

            client.sendMessage(new User().setId(2).setName("lisi").setAddress("nanchang").setMessage("message").setTitle("title"));

        } catch (TTransportException e) {
            e.printStackTrace();
        } catch (TException e) {
            e.printStackTrace();
        } finally {
            transport.close();
        }
    }
}
